package com.my.servlet;

import com.alibaba.fastjson.JSON;
import com.my.bean.Ticket;
import com.my.controal.JDBC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/SelectTicket")
public class SelectTicket extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理中文乱码
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        // 查询页面 获取用户输入的起始地与目的地
        String src = request.getParameter("source");
        String dst = request.getParameter("dst");
        String trainID = request.getParameter("trainID");
        // 判断根据目的地进行查找 还是 根据列车号查找
        String flag = request.getParameter("flag");

        String sql = null;

        ArrayList<Ticket> array = new ArrayList<Ticket>();

        try {

            // 根据起始目的地查找
            if(flag.equals("0")) {
                sql = String.format("SELECT * FROM tb_ticket WHERE tb_ticket.source = '%s' AND tb_ticket.dst = '%s'", src, dst);
            }
            else {
                sql = String.format("SELECT * FROM tb_ticket WHERE tb_ticket.trainID = '%s'", trainID);
            }

            ResultSet res = JDBC.Instance().selectTable(sql);

            if (res.next()) {
                // 记录不为空 所以将指针移动到头 开始遍历
                res.beforeFirst();

                while (res.next()) {
                    Ticket tk = new Ticket();

                    tk.setFlag(1);
                    tk.setTrainID(res.getString("trainID"));
                    tk.setSource(res.getString("source"));
                    tk.setDst(res.getString("dst"));
                    tk.setSourcetime(res.getString("sourcetime"));
                    tk.setDsttime(res.getString("dsttime"));
                    tk.setInventor(res.getInt("inventor"));
                    tk.setCostTime(res.getInt("costtime"));

                    String strCostTime = "";

                    // 根据分钟 转换为小时
                    if(tk.getCostTime() > 60) {
                        int hour = tk.getCostTime() / 60;
                        int min = tk.getCostTime() - (hour * 60);

                        strCostTime = String.format("%d小时%d分钟", hour, min);
                    }
                    else {
                        strCostTime = String.format("%d分钟", tk.getCostTime());
                }

                    tk.setStrCost(strCostTime);

                    array.add(tk);
                }
            }
            else {
                Ticket tk = new Ticket();

                tk.setFlag(0);
                array.add(tk);
            }

            String str = JSON.toJSONString(array);
            response.getWriter().print(str);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
